<?php
/**
 * @package VbWP
 * @version 1.6
 */
/*
Plugin Name: VbWP Beta
Plugin URI: http://wordpress.org/extend/plugins/hello-dolly/
Description: Wordpress - vBulletin Single Sign On
Author: Ambikuk, Ivan
Version: 0.1
Author URI: http://technolyze.net/
*/

define('VB_COOKIE_PREFIX', 'bb');
define('VB_TIMENOW', time());
define('VB_COOKIETIMEOUT', 900);

add_action('wp_logout', 'add_vb_logout');
add_action('wp_authenticate', 'add_vb_login');
add_filter('authenticate','add_vb_facebook_login');
add_action('user_register','add_vb_add_user');
add_action('delete_user', 'add_vb_delete_user');
add_action('profile_update','add_vb_update_user');

function add_vb_facebook_login(){
	global $wpdb;
	$cookie = sfc_cookie_parse();
	$fbuid=$cookie['user_id'];
	$user_id = $wpdb->get_var( $wpdb->prepare("SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'fbuid' AND meta_value = %s", $fbuid) );
	
	wpvb_set_login_cookies($user_id);

}
/*
 * login
 */
//add_action( 'wp_authenticate_user', 'add_vb_login' );

/*
 * logout
 */
include_once(ABSPATH . 'wp-includes/pluggable.php'); 
include_once ('wpvb.lib.php');
include_once ('wpvb.lib.inc.php');
include_once ('members.php');



add_action( 'init', 'add_vb_logout1' );
function add_vb_logout1() 
{
	if($_REQUEST["action"]=="logout"){
		$user = wp_get_current_user();
		wpvb_clear_cookies($user->user_id);
		wp_clear_auth_cookie();
//		wp_logout();
		wp_set_current_user(0);
		wp_redirect("http://nurbaya.voim.org");
		
	}
}

add_action( 'wp_logout', 'add_vb_logout' );
function add_vb_logout() 
{
	$user = wp_get_current_user();
	wpvb_clear_cookies($user->user_id);
	
}

function add_vb_login(){
	global $wpdb;
	$user = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->users WHERE user_login = '%s' ",$_POST['log']));
//	var_dump($user->ID);exit; 
	wpvb_set_login_cookies($user->ID);
}

function add_vb_add_user(){
	global $wpdb;
	$user = $wpdb->get_row("SELECT * FROM $wpdb->users order by ID desc");
	
	$post = $_POST;
	
	array_push($post,$user->ID);
	
	wpvb_create_user($post);
}

function add_vb_delete_user($user_id){
	wpdb_delete($user_id);
}

function add_vb_update_user($user_id){
//	var_dump($_POST);exit;
	wpdb_update($user_id);
}

/*
 * add hook login s2member
 */
add_filter("ws_plugin__s2member_login_redirect", "redirect_password_login_redirect", 10, 2);
function redirect_password_login_redirect($redirect, $vars = array('user' => null)) {

    if( isset($user->ID) ) {
        $changed_password = get_metadata("user", $user->ID, "changed_password",true);
        if( $changed_password != true ) {
            return get_bloginfo('url') . "/change-password/";
        } else {
            return $redirect;
        }
    }
}

/*
 * manage control
 */
add_action('admin_menu', 'wpvb_admin_menu');

function wpvb_admin_menu() {
add_menu_page('WPVb','WPVb Integration','manage_options','wpvb_admin', wpvb_admin_action);
}//end WP_easy_admin  function.
function wpvb_admin_action() {
?>
<div class="wrap">
<h1>WPVb Control</h1>
hehehehe
</div>
 
<?php
}
?>